<template>
  <!-- 广告管理 -->
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item label="标题">
        <el-input clearable placeholder="" style="width: 300px" v-model="searchForm.title"></el-input>
      </el-form-item>
      <el-form-item label="广告位置">
        <el-select v-model="searchForm.position" placeholder="请选择" style="width: 300px">
          <el-option v-for="(item, index) in position" :key="index" :label="item.key" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属小程序：">
        <el-select v-model="searchForm.project_id" placeholder="请选择" style="width: 300px">
          <el-option v-for="(item, index) in project" :key="index" :label="item.key" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态：">
        <el-select v-model="searchForm.status" placeholder="请选择" style="width: 300px">
          <el-option label="启用" value="1"></el-option>
          <el-option label="停用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建人:">
        <el-input clearable placeholder="" style="width: 300px" v-model="searchForm.created_user"></el-input>
      </el-form-item>
      <el-form-item label="创建时间：">
        <el-date-picker end-placeholder="结束日期" range-separator="至" start-placeholder="开始日期" type="daterange" v-model="searchForm.date" value-format="yyyy-MM-dd"> </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="getDataList()" icon="el-icon-search" type="primary">查询</el-button>
        <el-button size="mini" @click="refDataList()" icon="el-icon-refresh" type="warning">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="margin-bottom: 20px; text-align: right">
      <el-button size="mini" @click="addOrUpdateHandle()" type="primary">新增</el-button>
      <el-button size="mini" @click="handleDel()" type="danger">删除 </el-button>
    </div>
    <!-- 表格开始 -->
    <el-table :data="dataList" size="mini" border style="width: 100%" v-loading="dataListLoading" ref="multipleTable" @selection-change="handleSelectionChange">
      >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column align="center" header-align="center" label="编号" prop="typeName" width="80">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="图片" prop="img">
        <template slot-scope="scope">
          <span @click="handlePictureCardPreview('https://images.weserv.nl/?url=' + scope.row.img)"><img :src="'https://images.weserv.nl/?url=' + scope.row.img" alt="" width="100" height="100" /></span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="标题" prop="title"></el-table-column>
      <el-table-column align="center" header-align="center" label="广告位置" prop="position">
        <template slot-scope="scope">
          <span>{{ scope.row.position == 1 ? '首页banner' : '其他' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="所属小程序" prop="project__name">
        <!-- <template slot-scope="scope">{{scope.row.rolesId | levelDicFilter}}
        </template> -->
      </el-table-column>
      <el-table-column align="center" header-align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status ? '启用' : '停用' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="排序值" prop="sort"></el-table-column>
      <el-table-column align="center" header-align="center" label="创建人" prop="created_user"></el-table-column>
      <el-table-column align="center" header-align="center" label="创建时间" prop="created_time" width="200"></el-table-column>
      <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="addOrUpdateHandle(scope.row)" plain type="primary">修改</el-button>
          <el-button size="mini" @click="handleDel(scope.row)" plain type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      small
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      @current-change="currentChangeHandle"
      @size-change="sizeChangeHandle"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 10px; text-align: right"
    ></el-pagination>

    <!-- 图片大图 -->
    <el-dialog :visible.sync="VisibleIMg">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
    <!-- 弹窗, 新增 / 修改 -->
    <popup @refreshList="getDataList" ref="popup"></popup>
  </div>
</template>
<script>
import popup from './popup'
import { adList, noticebase, adDel } from '@/api/wxapplet/index.js'
export default {
  components: { popup },
  data() {
    return {
      VisibleIMg: false, //大图展示
      dialogImageUrl: '', //大图图片
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      searchForm: {
        title: '',
        position: '',
        project_id: '',
        status: '',
        created_user: '',
        date: [],
      },
      position: [], //广告位置
      project: [], //所属小程序
      dataListLoading: false,
      dataList: [],

      multipleSelection: [], //选中的数据
    }
  },
  mounted() {
    this.getDataList()
    noticebase({}).then((res) => {
      // 所属小程序
      this.project = res.data.project
      this.position = res.data.position
    })
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchForm)
      if (info.date.length) {
        info.start_time = info.date[0]
        info.end_time = info.date[1]
        delete info.date
      }
      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }
      adList(formData)
        .then((res) => {
          this.dataList = res.result.items
          this.totalPage = res.result.total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    //重置
    refDataList() {
      this.searchForm = {
        title: '',
        position: '',
        project_id: '',
        status: '',
        created_user: '',
        date: [],
      }
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 删除table
    handleDel(row) {
      if (row) {
        // 单个删除
        var info = Object.assign({ ids: [row.id] })
      } else {
        // 批量删除
        var info = { ids: [] }
        for (let key in this.multipleSelection) {
          info.ids.push(this.multipleSelection[key].id)
        }
      }
      if (info.ids.length <= 0) return this.$message.error('请至少选择一个')
      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }

      this.$confirm('确认删除')
        .then((_) => {
          adDel(formData)
            .then((res) => {
              this.$message.success('删除成功')
              this.getDataList()
            })
            .catch((e) => {
              this.$message.error('删除失败')
            })
        })
        .catch((_) => {})
    },
    // 更新值
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 新增 / 修改
    addOrUpdateHandle(row) {
      this.$refs.popup.open(row)
    },
    // 图片click
    handlePictureCardPreview(url) {
      this.dialogImageUrl = url
      this.VisibleIMg = true
    },
  },
}
</script>
<style lang="scss" scoped></style>
